package jsyf.oa.web.crm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ExportExcel;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.crm.ExhibitionCustomerInfoFacade;
import jsyf.oa.facade.crm.ExhibitionFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by WHH-PC on 2017/12/24.
 */
@Api(value = "展会接口", description = "展会接口")
@Controller
@RequestMapping("/exhibition")
public class ExhibitionController {
    @Resource
    private ExhibitionFacade exhibitionFacade;
    @Resource
    private ExhibitionCustomerInfoFacade exhibitionCustomerInfoFacade;


    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String id = exhibitionFacade.save(jsonData);
        JSONObject objResult = new JSONObject();
        objResult.put("id", id);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }


    @RequestMapping(value = "/getById.json", method = RequestMethod.POST)
    public void getById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String id = obj.getString("id");
        if (StringUtils.isBlank(id)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
            return;
        }
        JSONObject records = this.exhibitionFacade.getById(id);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }


    @RequestMapping(value = "/getByPage.json", method = RequestMethod.POST)
    public void getByPage(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        JSONObject records = this.exhibitionFacade.getExihibitionList(obj);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
        }
    }

    @RequestMapping(value = "/getExhibitionListByCompany.json", method = RequestMethod.POST)
    public void getExhibitionListByCompany(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String companyId = obj.getString("companyId");

        List<JSONObject> jsonObjectList = exhibitionFacade.getExhibitionListByCompany(companyId);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(jsonObjectList));
    }

    @RequestMapping(value = "/getReportData.json", method = RequestMethod.POST)
    public void getReportData(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String userId = obj.getString("userId");
        String exhibitionId = obj.getString("exhibitionId");
        List<JSONObject> jsonObjectList = exhibitionFacade.getReportData(userId, exhibitionId);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(jsonObjectList));
    }

    @RequestMapping(value = "/saveExihibitionCustomer.json", method = RequestMethod.POST)
    public void saveExihibitionCustomer(HttpServletResponse response, @RequestBody String jsonData) {
        String id = exhibitionCustomerInfoFacade.save(jsonData);
        JSONObject objResult = new JSONObject();
        objResult.put("id", id);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }


    @RequestMapping(value = "/getExihibitionCustomerById.json", method = RequestMethod.POST)
    public void getExihibitionCustomerById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String id = obj.getString("id");
        if (StringUtils.isBlank(id)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
            return;
        }
        JSONObject records = this.exhibitionCustomerInfoFacade.getById(id);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    @RequestMapping(value = "/getExhibitionCustomerInfo.json", method = RequestMethod.POST)
    public void getExihibitionCustomerInfo(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String userId = obj.getString("userId");
        String exhibitionId = obj.getString("exhibitionId");
        String key = obj.getString("key");
        Integer pageIndex = Integer.parseInt(obj.getString("pageIndex") == null ? "1" : obj.getString("pageIndex"));
        JSONObject jsonObject = exhibitionCustomerInfoFacade.getExhibitionCustomerInfo(pageIndex, userId, exhibitionId, key, false);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonObject));
    }

    @RequestMapping(value = "/getTrackRecordList.json", method = RequestMethod.POST)
    public void getTrackRecordList(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String cusId = obj.getString("cusId");
        List<JSONObject> jsonObjectList = exhibitionCustomerInfoFacade.getTrackRecordList(cusId);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(jsonObjectList));
    }

    @RequestMapping(value = "/saveExhibitionCustomerVisit.json", method = RequestMethod.POST)
    public void saveExhibitionCustomerVisit(HttpServletResponse response, @RequestBody String jsonData) {
        String id = exhibitionCustomerInfoFacade.saveExhibitionCustomerVisit(jsonData);
        JSONObject objResult = new JSONObject();
        objResult.put("id", id);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }

    @RequestMapping(value = "/getExhibitionIdByUserId.json", method = RequestMethod.POST)
    public void getExhibitionIdByUserId(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String userId = obj.getString("userId");
        List<JSONObject> jsonObjectList = exhibitionFacade.getExhibitionIdByUserId(userId);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(jsonObjectList));
    }

    @RequestMapping(value = "/getNomanagerRegion.json", method = RequestMethod.POST)
    public void getNomanagerRegion(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String userId = obj.getString("userId");
        List<JSONObject> jsonObjectList = exhibitionFacade.getNomanagerRegion(userId);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(jsonObjectList));
    }

    @RequestMapping(value = "/getSalerBySalerSupport.json", method = RequestMethod.POST)
    public void getSalerBySalerSupport(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String userId = obj.getString("userId");
        String userName = obj.getString("userName");
        List<JSONObject> jsonObjectList = exhibitionCustomerInfoFacade.getSalerBySalerSupport(userId, userName);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(jsonObjectList));
    }

    @RequestMapping(value = "/getCompanyExhibitionCount.json", method = RequestMethod.POST)
    public void getCompanyExhibitionCount(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        List<JSONObject> jsonObjectList = exhibitionFacade.getCompanyExhibitionCount();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(jsonObjectList));
    }

    @RequestMapping(value = "/outFile.json", method = RequestMethod.GET)
    public void outFile(HttpServletResponse response, @RequestParam String userId, @RequestParam String exhibitionId, @RequestParam String key) throws Exception {
        JSONObject customerInfo = exhibitionCustomerInfoFacade.getExhibitionCustomerInfo(1, userId, exhibitionId, key, true);

        String title = "展会客户列表";
        String[] rowsName = new String[]{"大区", "大区经理", "区域", "区域经理", "客户数量", "定金目标",
                "客户姓名", "客户电话", "意向机型", "报价（万）", "心理价格", "最新跟踪日期", "跟踪情况", "定金状态",
                "定金金额（万）", "陪同销售", "出行方式", "到达时间", "参会人数", "入住人数", "销售支持"};
        //导出数据
        List<Object[]> dataList = new ArrayList<Object[]>();
        List<JSONObject> customerInfoList = (List<JSONObject>) customerInfo.get("records");
        for (int i = 0; i < customerInfoList.size(); i++) {
            JSONObject jsonObject = customerInfoList.get(i);
            Object[] objects = new Object[]{
                    jsonObject.get("bigRegionName"),
                    jsonObject.get("bigUserName"),
                    jsonObject.get("regionName"),
                    jsonObject.get("userName"),
                    jsonObject.get("CUSTOM_NUM"),
                    jsonObject.get("DEPOSIT_NUM"),
                    jsonObject.get("NAME"),
                    jsonObject.get("TEL"),
                    jsonObject.get("modelName"),
                    jsonObject.get("quote"),
                    jsonObject.get("acceptPrice"),
                    jsonObject.get("TRACK_TIME"),
                    jsonObject.get("VISIT_CONTENT"),
                    jsonObject.get("depositStatusName"),
                    jsonObject.get("DEPOSIT"),
                    jsonObject.get("SALER_NAME"),
                    jsonObject.get("travelModelName"),
                    jsonObject.get("ARRIVALS_TIME"),
                    jsonObject.get("PARTICIPATION_NUM"),
                    jsonObject.get("STAY_NUM"),
                    jsonObject.get("supportName")
            };
            dataList.add(objects);
        }
        ExportExcel ex = new ExportExcel(title, rowsName, dataList, response);
        ex.export();
    }

    @RequestMapping(value = "/getExhibitionSummary.json", method = RequestMethod.POST)
    public void getExhibitionSummary(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> jsonObjectList = exhibitionFacade.getExhibitionSummary();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(jsonObjectList));
    }

    @RequestMapping(value = "/getBigCusDepart.json", method = RequestMethod.POST)
    public void getBigCusDepart(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String companyId = obj.getString("companyId");
        JSONObject jsonObjec = exhibitionFacade.getBigCusDepart(companyId);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonObjec));
    }
}
